package com.zymail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyClass;
import com.zymail.bean.ZyGood;
import com.zymail.bean.ZyGoodDe;
import com.zymail.bean.ZyGoodImg;
import com.zymail.service.ZyClassService;
import com.zymail.service.ZyGoodDeService;
import com.zymail.service.ZyGoodImgService;
import com.zymail.service.ZyGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/good")
public class GoodController extends BaseController{
    @Autowired
    public ZyGoodService goodService;
    @Autowired
    public ZyClassService zyClassService;
    @Autowired
    public ZyGoodDeService zyGoodDeService;
    @Autowired
    public ZyGoodImgService zyGoodImgService;

    /**
     * 返回分类列表的所有list
     * @return
     */
    @GetMapping("/getall")
    public List<ZyGood> getSortedList(){
        return goodService.getSortedGood();
    }

    /**
     * 返回分类列表的所有list
     * @return
     */
    @RequestMapping("/all")
    public Map<String,Object> all(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<ZyGood> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("id");
        List<ZyGood> zyGoods = goodService.list(wrapper);
        map.put("code",0);
        map.put("data",zyGoods);
        return map;
    }

    @RequestMapping("/save")
    public String save(ZyGood zyGood,Double price,Integer count,Integer deid,String url,String dename,Integer imgid){

        goodService.saveOrUpdate(zyGood);
        QueryWrapper<ZyGood> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("id").last("limit 1");
        ZyGood good = goodService.getOne(wrapper);

        ZyGoodImg zyGoodImg = new ZyGoodImg();
        zyGoodImg.setUrl(url);
        if (imgid!=null){
            zyGoodImg.setId(imgid);
        }
        zyGoodImg.setGoodId(good.getId());
        zyGoodImgService.saveOrUpdate(zyGoodImg);

        ZyGoodDe zyGoodDe = new ZyGoodDe();
        zyGoodDe.setCount(count);
        if (deid!=null){
            zyGoodDe.setId(deid);
        }
        zyGoodDe.setPrice(price);
        zyGoodDe.setName(dename);
        zyGoodDe.setGoodId(good.getId());
        zyGoodDeService.saveOrUpdate(zyGoodDe);

        return "succ";
    }

    @RequestMapping("/del")
    public String del(ZyGood zyGood){
        goodService.removeById(zyGood.getId());
        return "succ";
    }





    /**
     * 返回一个商品的信息
     * @param id
     * @return
     */
    @GetMapping("/getone/{id}")
    public ZyGood getOne(@PathVariable("id") Integer id){
        return goodService.getById(id);
    }


}
